exports.GiftGenericTableView = UI.ListView.subclass(
{
    className: "GiftGenericTableView",
  
	initialize: function($super, ref, type) 
	{
		$super();
		this.sections = null;
		this.storeItems = null;
		this._listSection = null;
		
		this._listSection = new Array();
		this._parentWindow = ref;
		var itemType = type;
		if(itemType == kGiftType.kGiftTypeStore)
		{
			this._giftItems = GAGameLoader.getStoreHandler().getAllItems(1 , 1 , true);
		}
		else if(itemType == kGiftType.kGiftTypeDecoration)
		{
			this._giftItems = GAGameLoader.getStoreHandler().getAllItems(2 , 1 , true);
		}
					
		this.setFrame([TCUtil.getAdjustedX(10),TCUtil.getAdjustedY(100),TCUtil.getAdjustedX(290),TCUtil.getAdjustedY(270)]);
		
		this.cellRowDelegates();
	},
	
    
    setSections: function($super, sections)
    {
        $super(sections);
    },

    cellForRowAtIndexPath:function(indexPath_Row)
    {
    	return this.createCellForMainStoreTable(indexPath_Row);
    },
    
	createCellForMainStoreTable: function(indexPath_Row)
	{
        var cellView = new GiftTableViewCell("storeCell1",this._giftItems[indexPath_Row],this._parentWindow);	
        return cellView;
	},
	    
	cellRowDelegates:function()
	{
		var temp = 0 , cell;
		var noOfCells = this._giftItems.length;
		this.storeItems = new Array();
		
		for (temp = 0; temp < noOfCells; temp++)
		{ 	
			if(this.isGiftableItem(this._giftItems[temp]))
			{
				cell = this.createCellForMainStoreTable(temp);	
				this.storeItems.push(cell);
			}
		}
		
		var listSection = new UI.ListViewSection({///equivalent of numberOfRowsInSection
			'rowHeight': TCUtil.getAdjustedY(90),///equivalent of heightForRowAtIndexPath
		});
		
		this._listSection.push(listSection);
		listSection.setItems(this.storeItems);
        this.setSections(this._listSection);
	},
	
	isGiftableItem: function(item)
	{
		if(item.is_giftable == true)
		{
			return true;
		}
		return false;
		
	},
	
	removeAllObjects: function()
	{
		var section = null;
        if(this._listSection) {
			if(this._listSection.length > 0)
			{
				section = this._listSection.pop();
				section.flush();
			}
        }

    	if(this.storeItems) {
            var length = this.storeItems.length;
    		for (var temp = 0; temp < length; temp++)
    		{
        		this.storeItems[temp].destroy();
				this.storeItems[temp] = null;
        	}
			delete this.storeItems;
			this.storeItems = null;
    	}
		if(section)
		{
			section.destroy();
			section = null;
		}
	},
												  
    destroy: function()
    {
    	if(this._listSection)
    	{
			if(this._listSection.length > 0)
			{
				this._listSection[0].flush();
			}
    	}
    	
    	if(this.storeItems)
    	{
    		var l = this.storeItems.length , i = 0;
			
	    	for(; i < l ; i++ )
	    	{
	    		var obj = this.storeItems.pop();
	    		obj.destroy();
	    	}
	    	delete this.storeItems;
	    	this.storeItems = null
    	}
		if(this._listSection)
    	{
			if(this._listSection.length > 0)
			{
				this._listSection[0].destroy();
			}
    		delete this._listSection;
    		this._listSection = null;
    	}    	
    },
});

